package com.wanmait.zaisousuo.util;

import java.util.ArrayList;
import java.util.List;

public class Pager {


    private int pageSize = 5;//每页显示条数，默认5
    private int dataCount;//数据总数
    private int pageNum;//当前页面
    private int pageCount;//总页数
    private int navigatepages=5;//导航页码数
    private List<Integer> navigatepageNums;//导航页页码集

    public Pager(Integer pageSize,Integer dataCount,Integer pageNum){
        this.pageSize = pageSize;
        this.pageNum = pageNum;
        this.dataCount = dataCount;
    }
//    获取导航页码数
    public List<Integer> getNavigatepageNums() {
        pageCount=this.getPageCount();

    //  总页数等于或者小于导航页码数
        if(pageCount<=navigatepages){
            navigatepageNums = new ArrayList<>();
            for (int i = 0;i<pageCount;i++) {
                navigatepageNums.add(i+1);
            }
        }
//        总页码数大于导航页码数
        else{
            Integer startNum = pageNum-navigatepages/2;//起始页码
            Integer endNum = pageNum+navigatepages/2;//结束页码
//          如果起始页码小于1
            if(startNum<=1){
                startNum=1;
                navigatepageNums = new ArrayList<>();
                for(int i = 0;i<navigatepages;i++){
                    navigatepageNums.add(i+1);
                }
            }
//          如果结束页码大于总页数
            if(endNum>pageCount){
                endNum=pageCount;
                navigatepageNums = new ArrayList<>();
                for (int i=pageCount-navigatepages;i<pageCount;i++){
                    navigatepageNums.add(i+1);
                }
            }
//            如过导航页码数在中间
            if(startNum>1&&endNum<=pageCount){
                navigatepageNums = new ArrayList<>();
                for (int i=startNum;i<endNum+1;i++){
                    navigatepageNums.add(i);
                }
            }
        }
        return navigatepageNums;
    }

    public int getInitRow() {
        return (this.getPageNum() - 1) * this.getPageSize();
    }

    public int getPageCount() {
        return (this.getDataCount() - 1) / this.getPageSize() + 1;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getDataCount() {
        return dataCount;
    }

    public void setDataCount(int dataCount) {
        this.dataCount = dataCount;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }
}
